package com.jzr.medical.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.BaseController;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.DateUtil;
import com.jzr.common.util.FileUtil;
import com.jzr.common.util.U;
import com.jzr.common.util.UrlUtil;
import com.jzr.medical.db1.model.TAppVersion;
import com.jzr.medical.db1.service.AppVersionService;
import com.jzr.medical.db1.vo.AppVersionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/appVersion")
public class AppVersionController extends BaseController {
    @Autowired
    AppVersionService appVersionService;

    @Value("${file.app-upload-folder}")
    private String appUploadFolder;

    @Value("${file.app-download-url}")
    private String appDownloadUrl;

    @GetMapping("/list")
    public Result<List<AppVersionVo>> list(){
        QueryWrapper<AppVersionVo> qw = new QueryWrapper<>();
        qw.orderByAsc("soft_type","full_version");
       List<AppVersionVo> list = appVersionService.list(qw);
       return Result.success("获取列表成功",list);
    }

    @PostMapping("/add")
    public Result<Boolean> add(TAppVersion appVersion) {
        Boolean rt = appVersionService.save(appVersion);
        return Result.success("成功",rt);
    }

    @GetMapping("/delete")
    public Result<Boolean> delete(Integer id) {
        TAppVersion appVersion = appVersionService.getById(id);
        String filename = appVersion.getSoftName();

        try {
            if(!U.isBlank(filename)) {
                File fullPath = new File(appUploadFolder, filename);
                if(fullPath.exists()){
                    fullPath.delete();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除文件失败");
        }
        Boolean rt = appVersionService.removeById(id);
        return Result.success("成功",rt);
    }

    @PostMapping("/update")
    public Result<Boolean> update(TAppVersion record) {
        Boolean rt = appVersionService.save(record);
        return Result.success("成功",rt);
    }

    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file, int softType,
                                 float fullVersion, String remark)  {
        if (file.isEmpty()) {
            return Result.fail("上传失败，请选择文件");
        }
        if(U.isBlank(fullVersion)){
            return Result.fail("上传失败，请输入版本号");
        }
        if(U.isBlank(remark) || remark.equalsIgnoreCase("undefined")){
            return Result.fail("上传失败，请输入描述");
        }

        String fileName = file.getOriginalFilename();
        String timeString = DateUtil.getNowTime(DateUtil.yyyyMMddHHmmss_FORMAT);
        fileName = FileUtil.appendSubfix(fileName,"_"+timeString);

        if(U.isBlank(appUploadFolder)){
            return Result.fail("获取不了app-folder");
        }
        try {
            File fullPath = new File(appUploadFolder);
            if (!fullPath.exists()) {
                return Result.fail("app-folder不存在," + fullPath.getAbsolutePath());
            }

            File dest = new File(fullPath, fileName);
            file.transferTo(dest);
        }catch (IOException e){
            e.printStackTrace();
            return Result.fail("保存文件失败");
        }

        try {
            TAppVersion appVersion = new TAppVersion();
            appVersion.setStatus(1);
            appVersion.setSoftName(fileName);
            appVersion.setFullVersion(fullVersion);
            appVersion.setVersion(fullVersion);
            appVersion.setRemark(remark);
            appVersion.setSoftType(softType);
            appVersion.setFolderUrl(appUploadFolder);
            appVersionService.save(appVersion);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("保存记录失败");
        }

        return Result.success("保存成功");
    }

    @GetMapping("/lastVersion")
    public Result<AppVersionVo> lastVersion(Integer softType ){
        if(softType == null){
            return Result.fail("请输入softType");
        }
        QueryWrapper<TAppVersion> qw = new QueryWrapper<>();
        qw.orderByDesc(TAppVersion.FullVersion_);
        qw.eq(TAppVersion.SoftType_,softType)
                .eq(TAppVersion.Status_,1);

        TAppVersion appVersion = appVersionService.getOne(qw);
        AppVersionVo appVersionVo = null;
        if(appVersion != null) {
            appVersionVo = new AppVersionVo();
            BeanUtils.copyProperties(appVersion, appVersion);
            String softName = appVersionVo.getSoftName() == null ? "" : appVersionVo.getSoftName();
            appVersionVo.setAccessUrl(UrlUtil.concat(appDownloadUrl, softName));
        }

        return Result.success("成功", appVersionVo);
    }


}
